package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


import  com.itheima.mapper.OrderSettingMapper;
import redis.clients.jedis.JedisPool;


@Service(interfaceClass = OderSettingService.class)
public class OderSettingServiceImpl implements OderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private  JedisPool jedisPool;

    @Override
    public List<Map> getOderSettingByMonth(String date) {

        String begin=date+"-1";
        String end=date+"-31";
        List<Map> list= orderSettingMapper.getOderSettingByMonth(begin,end);

        return list;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {

        ArrayList <OrderSetting> list=new ArrayList<>();
        list.add(orderSetting);

        this.add(list);

    }


    @Override
    @Transactional
    public void add(ArrayList<OrderSetting> list) {
        for (OrderSetting orderSetting : list) {

            if (jedisPool.getResource().sismember("odersetting",orderSetting.getOrderDate().toString())) {
                orderSettingMapper.update(orderSetting);
            } else {
                orderSettingMapper.add(orderSetting);
                jedisPool.getResource().sadd("odersetting",orderSetting.getOrderDate().toString());
            }

        }
    }
}

